package com.ioc.business.base.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ioc.business.base.domain.CustomerInfo;
import com.ioc.business.base.domain.StorageAreaLocation;
import com.ioc.business.base.domain.WarehouseUsage;
import com.ioc.business.base.domain.bo.warehouse.usage.WarehouseUsageBo;
import com.ioc.business.base.domain.bo.warehouse.usage.WarehouseUsageQueryBo;
import com.ioc.business.base.mapper.CustomerInfoMapper;
import com.ioc.business.base.mapper.WarehouseUsageMapper;
import com.ioc.business.base.service.IWarehouseUsageService;
import com.ioc.business.base.vo.warehouse.usage.WarehouseUsagePageVo;
import com.ioc.business.base.vo.warehouse.usage.WarehouseUsageVo;
import com.ioc.common.constant.UserConstants;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库使用情况表 服务实现类
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-09
 */
@Service
@AllArgsConstructor
public class WarehouseUsageServiceImpl extends ServiceImpl<WarehouseUsageMapper, WarehouseUsage> implements IWarehouseUsageService {

    private final CustomerInfoMapper customerInfoMapper;

    @Override
    public TableVo<WarehouseUsage, WarehouseUsagePageVo> selectWarehouseUsagePageList(WarehouseUsageQueryBo queryBo) {
        List<WarehouseUsage> result = baseMapper
                .queryPageList(queryBo);
        List<WarehouseUsagePageVo> pageVos = new ArrayList<>(result.size());
        if (CollectionUtil.isNotEmpty(result)) {
            pageVos = buildPageVos(result);
        }
        return new TableVo<>(result, pageVos);
    }

    private List<WarehouseUsagePageVo> buildPageVos(List<WarehouseUsage> records) {
        return records.stream().map(record -> {
            WarehouseUsagePageVo vo = new WarehouseUsagePageVo();
            BeanUtils.copyProperties(record, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public WarehouseUsageVo selectWarehouseUsageById(Long warehouseUsageId) {
        WarehouseUsage warehouseUsage = getById(warehouseUsageId);
        if (ObjectUtil.isNotEmpty(warehouseUsage)) {
            WarehouseUsageVo warehouseUsageVo = new WarehouseUsageVo();
            BeanUtils.copyProperties(warehouseUsage, warehouseUsageVo);
            return warehouseUsageVo;
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateWarehouseUsage(WarehouseUsageBo bo) {
        WarehouseUsage warehouseUsage = new WarehouseUsage();
        BeanUtils.copyProperties(bo, warehouseUsage);
        return updateById(warehouseUsage);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeWarehouseUsage(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return false;
        }
        List<WarehouseUsage> updateList = idList.stream().map(id -> {
            WarehouseUsage warehouseUsage = new WarehouseUsage();
            warehouseUsage.setWarehouseUsageId(id);
            warehouseUsage.setDelFlag(UserConstants.DEL_FLAG_REMOVED);
            return warehouseUsage;
        }).collect(Collectors.toList());
        return updateBatchById(updateList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editWarehouseUsage(List<StorageAreaLocation> storageAreaLocationList) {
        List<CustomerInfo> list = new LambdaQueryChainWrapper<>(customerInfoMapper)
                .eq(CustomerInfo::getDelFlag, UserConstants.DEL_FLAG_NORMAL)
                .list();
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        List<Long> customerIdList = list.stream().map(CustomerInfo::getCustomerId).collect(Collectors.toList());
        baseMapper.deleteByCustomerIdList(customerIdList);
        List<WarehouseUsage> warehouseUsageList = new ArrayList<>(storageAreaLocationList.size() * list.size());
        String username = SecurityUtils.getUsername(); // 获取当前用户名
        Date date = new Date(); // 获取当前日期
        for (CustomerInfo customerInfo : list) {
            warehouseUsageList.addAll(buildStorageAreaLocation(customerInfo, storageAreaLocationList, username, date));
        }
        saveBatch(warehouseUsageList);
    }

    private List<WarehouseUsage> buildStorageAreaLocation(CustomerInfo customerInfo,
                                                          List<StorageAreaLocation> storageAreaLocationList,
                                                          String username, Date date) {

        return storageAreaLocationList.stream().map(storageAreaLocation -> {
            WarehouseUsage warehouseUsage = new WarehouseUsage();
            warehouseUsage.setCustomerId(customerInfo.getCustomerId());
            warehouseUsage.setCustomerName(customerInfo.getCustomerName());
            warehouseUsage.setStorageAreaLocationId(storageAreaLocation.getStorageAreaLocationId());
            warehouseUsage.setStorageAreaType(storageAreaLocation.getStorageArea());
            warehouseUsage.setUsageAmount("0");
            warehouseUsage.setCreateBy(username);
            warehouseUsage.setCreateTime(date);
            warehouseUsage.setUpdateBy(username);
            warehouseUsage.setUpdateTime(date);
            return warehouseUsage;
        }).collect(Collectors.toList());
    }
}
